package com.jiuyin.dianjing.ui.activity.team;

import android.content.Intent;
import android.util.SparseBooleanArray;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.model.MatchGameItemModel;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.util.ToastUtil;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TYPE;
import static com.jiuyin.dianjing.api.constant.ApiConstant.VALUE_MATCH_TYPE_CREATE;

public class GameGridActivity extends BaseActivity {
    @BindView(R.id.gv_games)
    GridView gvGames;
    @BindView(R.id.bt_ok)
    Button btOK;
    List<MatchGameItemModel> games = new ArrayList<>();
    ArrayList<MatchGameItemModel> selectedGames = new ArrayList<>();
    private CommonAdapter<MatchGameItemModel> gamesAdapter;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_game_grid;
    }

    @Override
    protected void initData() {
        fetchGames();
        selectedGames = getIntent().getParcelableArrayListExtra("selected");
    }

    @Override
    protected void initView() {
        setTitle("选择游戏");
        gamesAdapter = new CommonAdapter<MatchGameItemModel>(this, R.layout.item_match_create, games) {
            @Override
            protected void convert(ViewHolder viewHolder, MatchGameItemModel item, int position) {
                viewHolder.setText(R.id.tv_match_create_name, item.getName());
                ImageView imageView = viewHolder.getView(R.id.img_match_create_icon);
                Glide.with(GameGridActivity.this).load(item.getLogo()).into(imageView);
                if (gvGames.isItemChecked(position)) {
                    viewHolder.setBackgroundRes(R.id.ll_game_info_item, R.drawable.item_match_game_select_select_bg);
                } else {
                    viewHolder.setBackgroundRes(R.id.ll_game_info_item, R.drawable.item_match_game_select_normal_bg);
                }
            }
        };
        gvGames.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
        gvGames.setAdapter(gamesAdapter);
        gvGames.setOnItemClickListener((parent, view, position, id) -> {
            gamesAdapter.notifyDataSetChanged();
            if (gvGames.getCheckedItemCount() > 5) {
                gvGames.setItemChecked(position, false);
                ToastUtil.showShort("最多选择5款游戏");
            }
        });
        btOK.setOnClickListener(v -> {
            SparseBooleanArray checked = gvGames.getCheckedItemPositions();
            selectedGames.clear();
            for (int i = 0; i < checked.size(); i++) {
                boolean b = checked.valueAt(i);
                if (b) {
                    int pos = checked.keyAt(i);
                    selectedGames.add(games.get(pos));
                }
            }
            Intent intent = getIntent().putParcelableArrayListExtra("selected", selectedGames);
            setResult(RESULT_OK, intent);
            finish();
        });
    }

    public void fetchGames() {
        HashMap<String, Object> params = new HashMap<>();
        params.put(KEY_TOKEN, HelperApplication.getToken());
        params.put(KEY_TYPE, VALUE_MATCH_TYPE_CREATE);
        JSONObject jsonObject = new JSONObject(params);
        getApiHelper().appGetMyCompetitionGameList(jsonObject.toString(), data -> {
            games.clear();
            games.addAll(data);
            if (selectedGames != null) {
                for (MatchGameItemModel selectedGame : selectedGames) {
                    for (int i = 0; i < games.size(); i++) {
                        MatchGameItemModel game = games.get(i);
                        if (game.getGameId().equals(selectedGame.getGameId())) {
                            gvGames.setItemChecked(i, true);
                            break;
                        }
                    }
                }
            }
            gamesAdapter.notifyDataSetChanged();
        });
    }

}
